package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    NursingLevelMapper nursingLevelMapper;


    @Override
    public PageBean<NursingLevelVo> nurselevelquery(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<NursingLevelVo> levelVos = nursingLevelMapper.nursinglevelquery(dto);
        PageBean<NursingLevelVo> of = PageBean.of(levelVos, NursingLevelVo.class);
        return of;
    }

    @Override
    public void add(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.add(nursingLevel);
    }

    @Override
    public NursingLevelVo selectid(Long id) {
        NursingLevelVo vo = nursingLevelMapper.selectid(id);
        return vo;
    }

    @Override
    public void update(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void delete(Long id) {
        NursingLevelVo selectid = nursingLevelMapper.selectid(id);
        if (selectid.getStatus()==1){
            throw new BaseException(BasicEnum.DELETE_LEVEL_STATUS_FAIL);
        }
        nursingLevelMapper.deleteid(id);
    }

    @Override
    public void status(Long id, Integer status) {
        NursingLevel build = NursingLevel.builder().id(id).status(status).build();
        nursingLevelMapper.update(build);
    }

    @Override
    public List<NursingLevelVo> selectall() {
        List<NursingLevelVo> vos= nursingLevelMapper.selectall();
        return vos;
    }
}


